// Copyright 2012 Google Inc. All Rights Reserved.

package com.google.appengine.syncserver.shared;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * This class represents a client id request. When client wants a client id,
 * it has to create this request object and the response from server will
 * contain the client id for this client.
 */
public class ClientIdRequest {
  
  private static final String USERNAME = "username"; 

  private static final Logger log = Logger.getLogger(ClientIdRequest.class.getName());

  /**
   * Converts serialized json string to {@link ClientIdRequest} object.
   * 
   * @param str
   * @return {@link ClientIdRequest} as read from the json string.
   */
  public static ClientIdRequest valueOf(String str)
      throws JsonParseException, JsonMappingException, IOException {
    return (ClientIdRequest) JsonUtils.stringToObject(str, ClientIdRequest.class);
  }

  private final String username;

  @JsonCreator
  public ClientIdRequest(@JsonProperty(USERNAME) String username) {
    this.username = username;
  }

  /**
   * @return The user name of the client.
   */
  @JsonProperty(USERNAME)
  public String getUsername() {
    return username;
  }

  @Override
  public String toString() {
    try {
      return JsonUtils.objectToString(this);
    } catch (JsonGenerationException e) {
      log.severe(e.getMessage());
    } catch (JsonMappingException e) {
      log.severe(e.getMessage());
    } catch (IOException e) {
      log.severe(e.getMessage());
    }
    return null;
  }
}
